package com.gomore.opple.module.addressdialog;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gomore.opple.R;
import com.gomore.opple.module.BaseFragment;
import com.gomore.opple.module.IntentStart;
import com.gomore.opple.utils.DensityUtil;
import com.gomore.opple.utils.DialogUtils;
import com.lljjcoder.citypickerview.widget.CityPicker;

import butterknife.Bind;
import butterknife.OnClick;


/**
 * Created by robot
 */
public class AddressDialogFragment extends BaseFragment implements AddressDialogContract.View {

    @Bind(R.id.address_bg)
    ImageView address_bg;
    @Bind(R.id.buttom_bg)
    RelativeLayout buttom_bg;
    @Bind(R.id.select_address)
    TextView select_address;
    @Bind(R.id.confirm)
    TextView confirm;

    private String storeCode;
    private AddressDialogContract.Presenter mPresenter;
    String province, citys, country;

    public static AddressDialogFragment getInstance() {
        return new AddressDialogFragment();
    }

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_address_dialog;
    }

    @Override
    protected void initalizeData() {
        if (getArguments() != null && getArguments().getString(IntentStart.STORECODE) != null) {
            storeCode = getArguments().getString(IntentStart.STORECODE);
        }
    }

    @Override
    protected void initalizeViews() {
        mPresenter.prepareInitData();
        int screenWidth = DensityUtil.screenWith(getActivity());
        //address_bg宽高设置
        int width = screenWidth * 9 / 10;
        int heigeht = width * 374 / 800;
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, heigeht);
        address_bg.setLayoutParams(layoutParams);
        buttom_bg.setLayoutParams(layoutParams);
    }


    @Override
    public void setPresenter(AddressDialogContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showProgressDialog() {
        DialogUtils.showLoadingDialog(getActivity(), getResources().getString(R.string.loading));
    }

    @Override
    public void hideProgressDialog() {
        DialogUtils.closeLoadingDialog();
    }

    @Override
    public void showMessage(String msg) {
        showToast(msg);
    }

    @Override
    public void showMessage(int msgId) {
        showToast(msgId);
    }

    @Override
    public void finishActivity() {
        getActivity().finish();
    }

    @OnClick({R.id.select_address, R.id.confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.select_address:
                ZonePicker();
                break;
            case R.id.confirm:
                if (TextUtils.isEmpty(province)) {
                    showMessage("请添加门店地址");
                    return;
                }
                if (storeCode != null) {
                    mPresenter.saveStoreAddress(storeCode, province, citys, country);
                }
                break;
        }
    }

    private void ZonePicker() {
        CityPicker cityPicker = new CityPicker.Builder(getActivity())
                .textSize(20)
                .title("地址选择")
                .backgroundPop(0xa0000000)
                .titleBackgroundColor("#cccccc")
                .titleTextColor("#000000")
                .confirTextColor("#666666")
                .cancelTextColor("#666666")
                .province("江苏省")
                .city("常州市")
                .district("天宁区")
                .textColor(Color.parseColor("#666666"))
                .provinceCyclic(true)
                .cityCyclic(false)
                .districtCyclic(false)
                .visibleItemsCount(5)
                .itemPadding(10)
                .onlyShowProvinceAndCity(false)
                .build();
        cityPicker.show();

        //监听方法，获取选择结果
        cityPicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() {
            @Override
            public void onSelected(String... citySelected) {
                //省份
                province = citySelected[0];
                //城市
                citys = citySelected[1].replace(" ", "");
                //区县（如果设定了两级联动，那么该项返回空）
                country = citySelected[2];
                //邮编
                String code = citySelected[3];
                select_address.setText(province + "-" + citys + "-" + country);
            }

            @Override
            public void onCancel() {
//                Toast.makeText(getActivity(), "已取消", Toast.LENGTH_LONG).show();
            }
        });
    }
}
